<template>
	<view style="padding: 10px;">
		<view>
			<u-form>
				<u-form-item label="日期:"  border-bottom="false" label-width="150" >
					<u-input v-model="formData.time" @click="show=!show" disabled="true" border="true"  />
				</u-form-item>
				<u-form-item label="工作站名称:"  border-bottom="false" label-width="150" >
					<zhilin-picker v-model="pickerShow" :datalist="gzzs" :multiple="false"
					@confirm="gzzSelect" :showSearch="true"></zhilin-picker>
					<u-input v-model="formData.gzzmc" @click="pickerShow=!pickerShow" :disabled="true" placeholder="请选择工作站"/>
					<u-icon class="arrowIcon" :name="arrowName" :class="[pickerShow ? 'arrowIconRotateRight' : 'arrowIconRotateDown']"></u-icon>
				</u-form-item>
				<u-form-item label="评分:"  border-bottom="false" label-width="150" >
					<slider :value="formData.result" @change="sliderChange" min="0" max="5" show-value block-size="20" />
				</u-form-item>
				<u-form-item label="备注:"  border-bottom="false"  label-width="150" >
					<u-input v-model="formData.mark" />
				</u-form-item>
				<u-form-item label="图片:"  border-bottom="false" label-width="150" >
					<u-upload ref="imgUpload" :name="name"  @on-uploaded="onUploaded" :action="action" @on-remove="onRemove" ></u-upload>
				</u-form-item>
			</u-form>
			<u-button @click="save()" :disabled="submitBtn" type="primary">保存</u-button>
		</view>
		<u-calendar v-model="show" :mode="mode" @change="change"></u-calendar>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import { mapState} from 'vuex';
	import zhilinPicker from "@/components/zhilin-picker/zhilin-picker.vue"
	export default {
		components:{zhilinPicker},
		computed:mapState(['user', ]),
		data() {
			return {
				isRatedData:[],
				pickerShow:false,
				arrowName:'arrow-down',
				submitBtn:false,
				action: this.$http.config.baseURL+'/gzznw/upload',
				name: 'files',
				gzzs: [],
				formData:{
					time: this.$util.getNowD(),
					gzzmc: '',
					result: '0',
					mark: '',
					location: '',
					lat: '',
					lng: '',
					filePaths: '',
					plr: ''
				},
				fileList: [],
				result: 0,
				step: 20,
				show: false,
				mode: 'date',
				
			}
		},
		methods:{	
			gzzSelect(value){
				this.formData.gzzmc = value;
			},
			change(e){
				this.formData.time  = e.result;
			},
			save(){
				const that = this;
				let files = [];
				for(let item of this.fileList){	
					if(item.progress==100){
						files = files.concat(item.response.data);
					}
				}
				that.submitBtn = true;
				this.formData.filePaths = files.join(",");
				this.formData.plr = this.user.user.id;
				this.$http.post('/gzznw/save',this.formData).then(res => {
					let data = res.data.data;
					if(data){
						that.$refs.uToast.show({
							title: '保存成功',
							type: 'success',
							callback: ()=>{
								that.$u.route({
									type:'back',
									delta: 1
								})
							}
						})
					}else{
						that.submitBtn = false;
					}
				}).catch(e => {
					that.submitBtn = false;
					that.$refs.uToast.show({
						title: '保存失败请稍后重试...',
						type: 'error'
					})
				});
			},
			onRemove(index, lists) {
				console.log('图片已被移除')
				this.fileList = lists;
			},
			onUploaded(lists) {
				console.log('onUploaded...');
				this.fileList = lists;
			},
			setLoaction(){
				let locations = this.$auth.getLocationInfo();
				console.log('locations......');
				console.log(locations);
				this.formData.lat = locations.lat;
				this.formData.lng = locations.lng;
				this.formData.location = locations.address;
			},
			//滑块操作
			sliderChange(e){
				this.formData.result = e.detail.value;
			},
			//检查已经评价过的数据（这里主要是已经评价过的工作站）
			checkIsRatedData(){
				this.$http.get("/gzznw/ratedData").then(res=>{
					this.isRatedData = res.data.data || [];
					this.gzzs = this.gzzs.filter(gzz => {
						return this.isRatedData.indexOf(gzz) == -1;
						});
				}).catch(e =>{
					
				})
			}
		},
		onReady(){
			this.checkIsRatedData();
		},
		onLoad() {
			this.setLoaction();
			this.gzzs = this.user.gzzs.concat();
		}
	}
</script>

<style scoped>
	.badge-button {
			padding: 4rpx 6rpx;
			background-color: #333333;
			color: #fff;
			border-radius: 10rpx;
			font-size: 22rpx;
			line-height: 1;
		}
	.arrowIcon{
		position: absolute;
		right: 10rpx;
		top: 40rpx;
	}
	
	.arrowIconRotateRight{
		transition: all .5s ease 0s;
		transform: rotate(-180deg);
	}
	
	.arrowIconRotateDown{
		transition: all .5s ease 0s;
		transform: rotate(0deg);
	}
</style>
